
package com.netease.xone.view;

import java.util.List;

import protocol.XoneCallBack;
import protocol.XoneService;
import protocol.meta.Source;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.netease.image.ImageType;
import com.netease.xone.R;
import com.netease.xone.activity.ActivitySourceDetail;
import com.netease.xone.widget.LoadingImageView;

public class HoriScrollSourceView extends HorizontalScrollView {

    private LinearLayout mContainer;
    private Context mContext;
    private int mScreenWidth;
    private List<Source> mSourceList;

    public HoriScrollSourceView(Context context) {
        this(context, null, 0);
    }

    public HoriScrollSourceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HoriScrollSourceView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        XoneService.getInstance().addListener(mCallBack);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mContainer = (LinearLayout)findViewById(R.id.sources_container);
        mContainer.setGravity(Gravity.CENTER_VERTICAL);
        mScreenWidth = mContext.getResources().getDisplayMetrics().widthPixels;
    }

    /**
     * 设置资讯源数据
     */
    public void setSources(List<Source> sourceList) {
        // TODO 设置资讯源的真实数据
        if (sourceList == null) {
            return;
        }
        mSourceList = sourceList;
        int count = sourceList.size();
        int showCount = mContext.getResources()
                .getInteger(R.integer.pgrecomm_horiscroll_source_num);// 一屏显示的个数
        int width = (int)(mScreenWidth / (showCount + .5f));
        int roundSize = mContext.getResources().getDimensionPixelSize(R.dimen.common_avatar_rounder);
        int avatarSize = mContext.getResources().getDimensionPixelSize(R.dimen.common_avatar_size);
        for (int i = 0; i < count; i++) {
            final Source source = sourceList.get(i);
            View sourceItem = View.inflate(mContext, R.layout.view_order_source_item, null);
            LoadingImageView sourceAvatar = (LoadingImageView)sourceItem.findViewById(R.id.source_avatar);
            sourceAvatar.setServerClipSize(avatarSize, avatarSize);
            if(!TextUtils.isEmpty(source.avatar)){
                sourceAvatar.setImageResource(R.drawable.avatar_source_default_round);
                sourceAvatar.setLoadingImage(source.avatar, ImageType.RoundMemCache, roundSize);
            }
            TextView title = (TextView)sourceItem.findViewById(R.id.source_item_title);
            TextView subscribeCount = (TextView)sourceItem
                    .findViewById(R.id.source_item_subscribe_number);
            title.setText(source.sourceName);
            subscribeCount.setText(source.subscribeCount + mContext.getString(R.string.subscribed));

            sourceItem.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    ActivitySourceDetail
                            .startActivity(mContext, source.sourceId, source.sourceName);
                }
            });
            mContainer.addView(sourceItem, width,
                    android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
        }
    }

    public void removeListener(){
        XoneService.getInstance().removeListener(mCallBack);
    }

    private XoneCallBack mCallBack = new XoneCallBack() {

        @Override
        public void onSubscribe(int transactionId, int[] subjectIdAndSourceId) {
            int sourceId = subjectIdAndSourceId[1];
            if (sourceId > 0 && mSourceList != null && mSourceList.size() > 0) {
                for (int i = 0; i < mSourceList.size(); i++) {
                    Source source = mSourceList.get(i);
                    if (source.sourceId == sourceId && !source.isSubscribed) {
                        source.isSubscribed = true;
                        source.subscribeCount++;
                        View sourceItemView = mContainer.getChildAt(i);
                        TextView subscribeCount = (TextView)sourceItemView
                                .findViewById(R.id.source_item_subscribe_number);
                        subscribeCount.setText(source.subscribeCount
                                + mContext.getString(R.string.subscribed));
                    }
                }
            }
        };

        @Override
        public void onUnsubscribe(int transactionId, int sourceId) {
            if (sourceId > 0 && mSourceList != null && mSourceList.size() > 0) {
                for (int i = 0; i < mSourceList.size(); i++) {
                    Source source = mSourceList.get(i);
                    if (source.sourceId == sourceId && source.isSubscribed) {
                        source.isSubscribed = false;
                        source.subscribeCount--;
                        View sourceItemView = mContainer.getChildAt(i);
                        TextView subscribeCount = (TextView)sourceItemView
                                .findViewById(R.id.source_item_subscribe_number);
                        subscribeCount.setText(source.subscribeCount
                                + mContext.getString(R.string.subscribed));
                    }
                }
            }
        };
    };

}
